/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function (candidates, target) {
    candidates = candidates.sort((a, b) => a - b)
    const path = []
    const res = []
    const len = candidates.length

    function backTrack(sum, startIndex) {
        if (sum === target) {
            res.push([...path])
            return
        }
        for (let i = startIndex; i < len; i++) {
            if (sum + candidates[i] > target) return
            path.push(candidates[i])
            backTrack(sum + candidates[i], i)
            path.pop()
        }
    }
    backTrack(0, 0)

    return res
};